package cn.org.timespace.easyframe.dao.permission.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import cn.org.timespace.easyframe.action.permission.OperationLogModel;
import cn.org.timespace.easyframe.dao.EfBaseDAO;
import cn.org.timespace.easyframe.dao.permission.OperationLogDao;
import cn.org.timespace.easyframe.model.permission.EfUser;
import cn.org.timespace.easyframe.model.permission.OperationLog;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;
import cn.org.timespace.easyframe.util.pager.Pager;

/**
 * <p>
 * 用户操作日志接口实现类
 * </p>
 * 
 * @version 1.0, 11/01/18
 * @since 1.0
 * @author 
 */

public class OperationLogDaoImpl extends EfBaseDAO implements
		OperationLogDao {

	public List<OperationLog> findByCondition(OperationLogModel model) {
		log.debug("finding all OperationLog instances");
		try {
			StringBuffer queryString = new StringBuffer("from OperationLog a where 1=1 ");
			
			if(!StringUtils.isEmpty(model.getOperatorId())){
				queryString.append("and a.operatorId = '"+model.getOperatorId().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getDicModel())){
				queryString.append("and a.dicModel = '"+model.getDicModel().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getUserName())){
				queryString.append("and a.efUser.userName like '%"+model.getUserName().trim()+"%' ");
			}
			
			if(!StringUtils.isEmpty(model.getDicFunCode())){
				queryString.append("and a.dicFunCode = '"+model.getDicFunCode()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getOperationCode())){
				queryString.append("and a.operationCode = '"+model.getOperationCode()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getContent())){
				queryString.append("and a.content like '%"+model.getContent().trim()+"%' ");
			}
			
			if(!StringUtils.isEmpty(model.getOperationDateStart()) && !StringUtils.isEmpty(model.getOperationDateEnd())){
				queryString.append("and a.operationDate >= :operationDateStart ");
				queryString.append("and a.operationDate <= :operationDateEnd ");
			}else if(!StringUtils.isEmpty(model.getOperationDateStart())){
				queryString.append("and a.operationDate >= :operationDateStart ");
			}else if(!StringUtils.isEmpty(model.getOperationDateEnd())){
				queryString.append("and a.operationDate <= :operationDateEnd ");
			}
			
			Query query = getSessionFactory().openSession().createQuery(queryString.toString());
			
			if(!StringUtils.isEmpty(model.getOperationDateStart()) && !StringUtils.isEmpty(model.getOperationDateEnd())){
				query.setDate("operationDateStart", java.sql.Date.valueOf(model.getOperationDateStart()));
				query.setDate("operationDateEnd", java.sql.Date.valueOf(model.getOperationDateEnd()));
			}else if(!StringUtils.isEmpty(model.getOperationDateStart())){
				query.setDate("operationDateStart", java.sql.Date.valueOf(model.getOperationDateStart()));
			}else if(!StringUtils.isEmpty(model.getOperationDateEnd())){
				query.setDate("operationDateEnd", java.sql.Date.valueOf(model.getOperationDateEnd()));
			}
			
			queryString.append(" order by a.operatorId desc ");
			
			List<OperationLog> resList = query.list();
			return resList;
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	

	public List<Object> findByCondition(OperationLogModel model, Pager p) {
		log.debug("finding all OperationLog instances");
		try {
			StringBuffer queryString = new StringBuffer("from OperationLog a where 1=1 ");
			
			if(!StringUtils.isEmpty(model.getOperatorId())){
				queryString.append("and a.operatorId = '"+model.getOperatorId().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getDicModel())){
				queryString.append("and a.dicModel = '"+model.getDicModel().trim()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getUserName())){
				queryString.append("and a.efUser.userName like '%"+model.getUserName().trim()+"%' ");
			}
			
			if(!StringUtils.isEmpty(model.getDicFunCode())){
				queryString.append("and a.dicFunCode = '"+model.getDicFunCode()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getOperationCode())){
				queryString.append("and a.operationCode = '"+model.getOperationCode()+"' ");
			}
			
			if(!StringUtils.isEmpty(model.getContent())){
				queryString.append("and a.content like '%"+model.getContent().trim()+"%' ");
			}
			
			if(!StringUtils.isEmpty(model.getOperationDateStart()) && !StringUtils.isEmpty(model.getOperationDateEnd())){
				queryString.append("and a.operationDate >= :operationDateStart ");
				queryString.append("and a.operationDate <= :operationDateEnd ");
			}else if(!StringUtils.isEmpty(model.getOperationDateStart())){
				queryString.append("and a.operationDate >= :operationDateStart ");
			}else if(!StringUtils.isEmpty(model.getOperationDateEnd())){
				queryString.append("and a.operationDate <= :operationDateEnd ");
			}
			
			Query query = getSessionFactory().openSession().createQuery(queryString.toString());
			
			if(!StringUtils.isEmpty(model.getOperationDateStart()) && !StringUtils.isEmpty(model.getOperationDateEnd())){
				query.setDate("operationDateStart", java.sql.Date.valueOf(model.getOperationDateStart()));
				query.setDate("operationDateEnd", java.sql.Date.valueOf(model.getOperationDateEnd()));
			}else if(!StringUtils.isEmpty(model.getOperationDateStart())){
				query.setDate("operationDateStart", java.sql.Date.valueOf(model.getOperationDateStart()));
			}else if(!StringUtils.isEmpty(model.getOperationDateEnd())){
				query.setDate("operationDateEnd", java.sql.Date.valueOf(model.getOperationDateEnd()));
			}
			
			queryString.append(" order by a.operatorId desc ");
			
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			
			setQueryPager(query, p);
			resList.add(query.list());
			
			return resList;
			
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}		
		
	}

	public void save(OperationLog transientInstance) {
		log.debug("saving OperationLog instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void save(String dicMode, String dicFunCode, String operationCode,
			String content, String remark, String userId) {
		
		OperationLog operationLog = new OperationLog();
		
		operationLog.setOperatorId(IdentityGenerator.getInstance().getId());
		operationLog.setDicModel(dicMode);  //模块
		operationLog.setDicFunCode(dicFunCode); //功能代码
		operationLog.setOperationCode(operationCode); //操作代码
		operationLog.setContent(content); //内容
		EfUser efUser = new EfUser();
		efUser.setUserId(userId);
		operationLog.setEfUser(efUser);
		operationLog.setOperationDate(new Date());
		operationLog.setRemark(remark);
		
		this.save(operationLog);
	}

}
